﻿using Authentication.Model.Model;
using Authentication.ServiceCommand;
using Authentication.Web.Service.Core;
using Common.Contract;
using Common.Contract.Authentication;
using Common.Contract.SystemConfig;
using Common.Contract.SystemConfig.Record;
using Common.Contract.SystemConfig.Request;
using Common.Contract.SystemConfig.Response;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Authentication.Web.Service
{
    public class ConfigurationService : ServiceBase, IConfiguration
    {
        public SearchDictionaryResponse SearchDictionary(SearchDictionaryRequest request)
        {
            SearchDictionaryResponse response = new SearchDictionaryResponse { IsSuccess = true, ErrorMessage = "" };
            if (request == null || request.Header == null || request.Header.ApplicationKey == null)
            {
                response.ErrorMessage = "Invalidate the request header";
                response.IsSuccess = false;
                return response;
            }


            List<DictionaryRecord> records = new List<DictionaryRecord>();

            //Check by Keys
            var diction = Provider.Configurations.RetrieveDictionary(request.Header.ApplicationKey.Value, request.Key);
            if (diction != null)
            {
                DictionaryRecord record = new DictionaryRecord
                {
                    ApplicationKey = new Guid(diction.ApplicationKey),
                    Key = diction.Key,
                    CreationDate = diction.CreationDate,
                    UpdateDate = diction.UpdateDate,
                    Value = diction.Value
                };

                records.Add(record);
            }

            //Check by LifeTime
            if (request.IsOutTime)
            {
                var dictions = Provider.Configurations.SearchDictionaries(request.Header.ApplicationKey.Value, request.MaxMinuteLife);
                if (dictions != null)
                {
                    var tmprecords = dictions.Select(x => new DictionaryRecord
                    {
                        ApplicationKey = new Guid(x.ApplicationKey),
                        Key = x.Key,
                        CreationDate = x.CreationDate,
                        UpdateDate = x.UpdateDate,
                        Value = x.Value
                    });

                    records.AddRange(tmprecords);
                }
            }

            //result
            if (records == null || records.Count == 0)
            {
                response.ErrorMessage = string.Format("Can not found dictionary with ApplicationKey={0} in lifetime={1}", request.Header.ApplicationKey, request.MaxMinuteLife);
                response.IsSuccess = false;
                return response;
            }
            response.Records = records;
            return response;
        }

        public ResponseRecord DeleteDictionary(DeleteDictionaryRequest request)
        {
            ResponseRecord response = new ResponseRecord { IsSuccess = true, ErrorMessage = "" };
            if (request == null || request.Header == null || request.Header.ApplicationKey == null)
            {
                response.ErrorMessage = "Invalidate the request header";
                response.IsSuccess = false;
                return response;
            }

            var diction = Provider.Configurations.DeleteDictionaries(request.Header.ApplicationKey.Value, request.Keys.ToArray());
            if (diction == null || diction.Length == 0)
            {
                response.ErrorMessage = string.Format("Can not found dictionary with ApplicationKey={0} in key=({1})", request.Header.ApplicationKey, string.Join(",", request.Keys.ToArray()));
                response.IsSuccess = false;
                return response;
            }
            return response;
        }

        public ResponseRecord AddDictionary(AddDictionaryRequest request)
        {
            ResponseRecord response = new ResponseRecord { IsSuccess = true, ErrorMessage = "" };
            if (request == null || request.Header == null || request.Header.ApplicationKey == null || request.Record == null)
            {
                response.ErrorMessage = "Invalidate the request header";
                response.IsSuccess = false;
                return response;
            }

            var dictAdd = new Dictionary
            {
                ApplicationKey = request.Record.ApplicationKey.ToString().ToUpper(),
                CreationDate = request.Record.CreationDate ?? DateTime.Now,
                Key = request.Record.Key,
                UpdateDate = request.Record.UpdateDate,
                Value = request.Record.Value
            };

            var diction = Provider.Configurations.AddDictionary(dictAdd);
            if (diction == null)
            {
                response.ErrorMessage = string.Format("Can not add dictionary with ApplicationKey={0} in key=(1)", request.Header.ApplicationKey, request.Record.Key);
                response.IsSuccess = false;
                return response;
            }
            return response;
        }


        public RetrieveDictionaryResponse RetrieveDictionary(RetrieveDictionaryRequest request)
        {
            RetrieveDictionaryResponse response = new RetrieveDictionaryResponse { IsSuccess = true, ErrorMessage = "" };
            //Check by Keys
            var diction = Provider.Configurations.RetrieveDictionary(request.Header.ApplicationKey.Value, request.Key);
            if (diction != null)
            {
                DictionaryRecord record = new DictionaryRecord
                {
                    ApplicationKey = new Guid(diction.ApplicationKey),
                    Key = diction.Key,
                    CreationDate = diction.CreationDate,
                    UpdateDate = diction.UpdateDate,
                    Value = diction.Value
                };
                response.Record = record;
                return response;
            }

            response.IsSuccess = false;
            response.ErrorMessage = string.Format("Can not retrieve dictionary with ApplicationKey={0} in key=(1)", request.Header.ApplicationKey, request.Key);
            return response;
        }


        public SearchApplicationErrorResponse SearchApplicationError(SearchApplicationErrorRequest request)
        {
            return new SearchAppErrorServiceCommand(Provider).Execute(request);
        }

        public ResponseRecord UpdateApplicationError(UpdateApplicationErrorRequest request)
        {
            return new UpdateApplicationErrorCommand(Provider).Execute(request);
        }
    }
}